from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext, loader
from booktest.models import BookInfo

# Create your views here.


# 访问127.0.0.1:8000则直接页面显示"index" ----> 对应url(r'^$', views.index)
# def index(request):
#     return HttpResponse("index")

# 访问127.0.0.1:8000则显示页面index.html  ----> 对应url(r'^$', views.index)
def index(request):
    return render(request, 'booktest/index.html')

# 访问127.0.0.1:8000/112233直接页面显示"detail 112233" --->对应url(r'^([0-9]+)/$', views.detail)
def detail(request,id):
    return HttpResponse("detail %s" % id)


# 等价于transfer_arg, 只不过采用的是原始版的编写方法
def transfer_arg2(request):
    booklist = BookInfo.objects.all()
    book1 = booklist[0]
    template = loader.get_template('booktest/transfer_arg.html')
    context = RequestContext(request, {'book': book1})
    return HttpResponse(template.render(context))


# 前端用{{book.btitle}}的方式来显示context
def transfer_arg(request):
    booklist = BookInfo.objects.all()
    book1 = booklist[0]
    context = RequestContext(request, {'book': book1})
    return render(request, 'booktest/transfer_arg.html', context)


# 前端用{%for book in booklist%} {%endfor%}的方式来显示context
def transfer_list(request):
    booklist = BookInfo.objects.all()
    context = RequestContext(request, {'booklist': booklist})
    return render(request, 'booktest/transfer_list.html', context)